package com.wise.wing.activity.main;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.bm.corelibs.AppManager;
import com.bm.corelibs.logic.BaseLogic.NListener;
import com.makeramen.roundedimageview.RoundedTransformationBuilder;
import com.squareup.picasso.Picasso;
import com.wise.wing.R;
import com.wise.wing.activity.accountsecurity.AccountSecurityActivity;
import com.wise.wing.activity.logic.AdManager;
import com.wise.wing.activity.logic.LoginManager;
import com.wise.wing.activity.login.LoginActivity;
import com.wise.wing.activity.mine.MessageAnnouncementActivity;
import com.wise.wing.activity.mysites.MySitesActivity;
import com.wise.wing.activity.mywallet.MyWalletActivity;
import com.wise.wing.activity.order.MyOrderActivity;
import com.wise.wing.activity.settting.PersonInfoActivity;
import com.wise.wing.activity.settting.SettingActivity;
import com.wise.wing.bean.BaseData;
import com.wise.wing.bean.Dispatcher;
import com.wise.wing.constant.Constant;
import com.wise.wing.views.CommonNavigationBar;

public class MainActivity extends BaseActivity implements OnClickListener {

	private DrawerLayout drawerLayout;
	private boolean isShow = false;
	private static MainActivity instance;

	private LinearLayout menu_1, menu_2, menu_3, menu_4, menu_5, menu_6, menu_7;
	private LinearLayout ll_find1, ll_find2, ll_find3;
	private RelativeLayout rl_parent;
	private ImageView headview_image;
	private TextView headview_uid, headview_tel;
	private ImageView iv_ad;

	private AdManager adManager;

	private CommonNavigationBar navi;

	private String imageSrc;
	
	private OnClickListener mineListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if (LoginManager.getInstance(mActivity).isLogin())
				showMenuList();
			else
				startActivity(new Intent(mActivity, LoginActivity.class));
		}
		
	};
	
	public MainActivity() {
		instance = this;
	}

	public static synchronized MainActivity getInstance() {
		return instance;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);
		findViews();
		//RequestManager.init(mActivity, Constant.VOLLEY_CACHE_PATH);
		init();
		addListeners();
		

	}

	@Override
	protected void onResume() {
		super.onResume();
		if (!LoginManager.getInstance(mActivity).isLogin()) {
			drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
		} else {
			drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
		}
		setUserInfo(LoginManager.getInstance(mActivity).getSavedUser());
	}

	private void setUserInfo(Dispatcher user) {
		// user.icon =
		// "https://d13yacurqjgara.cloudfront.net/users/109605/screenshots/2152255/lion.png";
		if (user.icon != null && user.icon.length() > 0) {
			Picasso.with(mActivity).load(user.icon).resize(480, 480).centerCrop().transform(new RoundedTransformationBuilder().oval(true).build()).into(headview_image);
		}

		headview_uid.setText("UID " + user.disId);
		headview_tel.setText("手机号 " + user.phone);
	}

	@Override
	public void findViews() {
		navi = (CommonNavigationBar) findViewById(R.id.navi);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		
	

		drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		menu_1 = (LinearLayout) findViewById(R.id.menu_1);
		menu_2 = (LinearLayout) findViewById(R.id.menu_2);
		menu_3 = (LinearLayout) findViewById(R.id.menu_3);
		menu_4 = (LinearLayout) findViewById(R.id.menu_4);
		menu_5 = (LinearLayout) findViewById(R.id.menu_5);
		menu_6 = (LinearLayout) findViewById(R.id.menu_6);
		menu_7 = (LinearLayout) findViewById(R.id.menu_7);

		rl_parent = (RelativeLayout) findViewById(R.id.rl_parent);
		headview_image = (ImageView) findViewById(R.id.headview_image);
		headview_tel = (TextView) findViewById(R.id.headview_tel);
		headview_uid = (TextView) findViewById(R.id.headview_uid);
		iv_ad = (ImageView) findViewById(R.id.iv_ad);

		ll_find1 = (LinearLayout) findViewById(R.id.ll_find1);
		ll_find2 = (LinearLayout) findViewById(R.id.ll_find2);
		ll_find3 = (LinearLayout) findViewById(R.id.ll_find3);
	}

	@Override
	public void init() {
		navi.setTitle(R.string.app_name);
		navi.showLeftMineBtn(mineListener);
		adManager = new AdManager();
		adManager.getImage(listener);
	}

	public void showMenuList() {
		drawerLayout.openDrawer(Gravity.LEFT);
		isShow = true;
	}

	public void hideMenuList() {
		drawerLayout.closeDrawers();
		isShow = false;
	}

/*	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
*/
	
	

	
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		// 找车
		case R.id.ll_find1:
			intent = new Intent(mActivity, FindTrucksActivity.class);
			intent.putExtra(FindTrucksActivity.TRUCK_TYPE, 0);
			startActivity(intent);
			break;

		// 找车
		case R.id.ll_find2:
			intent = new Intent(mActivity, FindTrucksActivity.class);
		intent.putExtra(FindTrucksActivity.TRUCK_TYPE, 1);
			startActivity(intent);
			break;

		// 找车
		case R.id.ll_find3:
			intent = new Intent(mActivity, FindTrucksActivity.class);
		intent.putExtra(FindTrucksActivity.TRUCK_TYPE, 2);
			startActivity(intent);
			break;

		// 我的主页
		case R.id.menu_1:
			hideMenuList();
			break;

		// 设置
		case R.id.menu_7:
			startActivity(new Intent(MainActivity.this, SettingActivity.class));
			// hideMenuList();
			break;
		case R.id.iv_ad:
			if (imageSrc != null && imageSrc.length() > 4) {
				intent = new Intent(mActivity, WebActivity.class);
				intent.putExtra("url", imageSrc);
				startActivity(intent);
			}
			break;
		case  R.id.iv_back:
			hideMenuList();
			break;
		}
		
	}

	private OnClickListener personalClickListener = new OnClickListener() {
		Intent intent;

		@Override
		public void onClick(View v) {
			// hideMenuList();

			switch (v.getId()) {
			// 个人资料
			case R.id.rl_parent:
				intent = new Intent(mActivity, PersonInfoActivity.class);
				startActivity(intent);
				break;

			// 我的订单
			case R.id.menu_2:
				intent = new Intent(mActivity, MyOrderActivity.class);
				startActivity(intent);
				break;

			// 我的站点
			case R.id.menu_3:
				intent = new Intent(mActivity, MySitesActivity.class);
				startActivity(intent);
				break;

			// 我的钱包
			case R.id.menu_4:
				intent = new Intent(mActivity, MyWalletActivity.class);
				startActivity(intent);
				break;

			// 账户安全
			case R.id.menu_5:
				intent = new Intent(mActivity, AccountSecurityActivity.class);
				startActivity(intent);
				break;

			// 消息公告
			case R.id.menu_6:
				intent = new Intent(mActivity, MessageAnnouncementActivity.class);
				startActivity(intent);
				break;
			}

		}
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (isShow) {
				drawerLayout.closeDrawers();
				isShow = false;
				return false;
			} else {
				doublePressBackToast();
			}
			return true;
		} else {
			return super.onKeyUp(keyCode, event);
		}
	}

	private boolean isBackPressed;

	private void doublePressBackToast() {
		if (!isBackPressed) {
			isBackPressed = true;

			Toast.makeText(this, "再次点击返回退出程序", Toast.LENGTH_SHORT).show();
		} else {
			AppManager.getAppManager().appExit();
		}

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				isBackPressed = false;
			}
		}, 2000);
	}

	NListener<BaseData> listener = new NListener<BaseData>() {

		@Override
		public void onResponse(BaseData data) {
			if (data.status == Constant.STATUS_OK) {

				imageSrc = data.data.adverList.get(0).linkSrc;
				Picasso.with(mActivity).load(data.data.adverList.get(0).img).into(iv_ad);
			}
		}

		@Override
		public void onErrResponse(VolleyError error) {

		}

		@Override
		public void onAllPageLoaded(int nowPage, int totalPage) {

		}

	
	};
	private ImageView iv_back;
	private RecommendAdFragment fr_ad;
	

	@Override
	public void addListeners() {
		// TODO Auto-generated method stub
		ll_find1.setOnClickListener(this);
		ll_find2.setOnClickListener(this);
		ll_find3.setOnClickListener(this);
		iv_back.setOnClickListener(this);

		menu_1.setOnClickListener(this);
		menu_2.setOnClickListener(personalClickListener);
		menu_3.setOnClickListener(personalClickListener);
		menu_4.setOnClickListener(personalClickListener);
		menu_5.setOnClickListener(personalClickListener);
		menu_6.setOnClickListener(personalClickListener);
		menu_7.setOnClickListener(this);
		rl_parent.setOnClickListener(personalClickListener);
		iv_ad.setOnClickListener(this);
		
		
		
	}

	

}
